﻿using AyaWo.Commen;
using AyaWo.DAL;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BellesLettres
{
    public class BellesLettresSpler : ISpler,Spler
    {
        Queue<DM> qdm = new Queue<DM>();
        bool _lock = true;
        public BellesLettresSpler()
        {
            this.DatName = "BellesLettres";
            this.SplerName = "散文网爬虫";
        }
        //AddContent
        /// <summary>
        /// 开始执行
        /// </summary>
        public void Initiali()
        {
            try
            {
                if (_lock)
                {
                    _lock = false;
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    //dic.Add("/jingdian/youmei/", "优美");
                    //dic.Add("/jingdian/weimei/", "唯美句子");
                    dic.Add("/jingdian/shanggan/", "伤感的句子");
                    //dic.Add("/jingdian/", "经典语句");
                    dic.Add("/jingdian/aiqing/", "爱情句子");
                    for (int i = 1; i <= 10; i++)
                    {
                        dic.Add("/sanwen/love/?p=" + i, "爱情散文");
                        dic.Add("/sanwen/shanggan/?p=" + i, "伤感散文");
                        dic.Add("/sanwen/zheli/?p=" + i, "哲理散文");
                        dic.Add("/suibi/shenghuo/?p=" + i, "生活随笔");
                        dic.Add("/sanwen/qinqing/?p=" + i, "亲情");
                        dic.Add("/shige/xiandai/?p=" + i, "现代诗歌");
                    }
                    foreach (var t in dic)
                    {
                        ListUrl(t.Key, t.Value);
                    }
                    foreach (DM dm in qdm)
                    {
                        spidercontent content = GetContent(dm);
                        if (content == null||content.Title.IsNullOrWhiteSpace()||content.Content.IsNullOrWhiteSpace())
                            return;
                        if (spcontentService.LinkCanInserts(dm.Url, 1))
                        {
                            spcontentService.AddContent(content);
                        }
                    }
                    _lock = true;
                }
            }
            catch (Exception ex)
            {
                Logser.Error(this.SplerName+"-Initiali: " + ex.ToString());
            }
        }

        spidercontent GetContent(DM dm)
        {
            try
            {
                if (dm == null)
                    return null;
                if (!spcontentService.CanInsert(dm.Url))
                {
                    return null;
                }
                var NO = CommonHelper.CreateNo();
                spidercontent sp = new spidercontent() { ID = NO, Source= "散文", Link = dm.Url, CountType = dm.pageName, CreateTime = DateTime.Now, typ = 1 };
                string docText = HtmlHelper.GetHtml(dm.Url);
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);
                if (doc == null)
                    return null;

                var title = doc.DocumentNode.SelectSingleNode("//div[@class=\"title\"]").InnerText;
                sp.Title = title;

                var auto = doc.DocumentNode.SelectSingleNode("//div[@class=\"info\"]//a").InnerText;
                sp.Autos = auto;

                var time = doc.DocumentNode.SelectSingleNode("//div[@class=\"info\"]").InnerText;
                if (time.NotNullOrWhiteSpace())
                {
                    var ind = time.IndexOf('&');
                    sp.BuildTime = time.Substring(0, ind).Trim();
                }

                var contentHtml = doc.DocumentNode.SelectSingleNode("//div[@class=\"content\"]");
                ///处理下载图片
                HtmlNodeCollection img = doc.DocumentNode.SelectNodes("//div[@class=\"content\"]//img");
                if (img != null && img.Count() > 0)
                {
                    foreach (HtmlNode child in img)
                    {
                        string imgurl = child.Attributes["src"].Value;
                        var ty = imgurl.Split('/');
                        var imgName = imgurl.Split('/')[ty.Length - 1];
                        string newsrc = HtmlHelper.DownLoadImg(imgurl, imgName, NO);
                        child.SetAttributeValue("src", newsrc);
                    }
                }
                var a = doc.DocumentNode.SelectNodes("//div[@class=\"content\"]//a");
                if (a != null && a.Count() > 0)
                {
                    foreach (HtmlNode child in a)
                    {
                        child.SetAttributeValue("href", "www.qqban.top");
                    }
                }
                var content = contentHtml.InnerHtml.Replace("首发散文网", "").Replace("https://www.sanwen.net", "");
                sp.Content = content;
                return sp;
            }
            catch (Exception ex)
            {
                Logser.Error(this.SplerName+"-GetContent: " + ex.Message);
                return null;
            }
        }

        #region 获取所有的内容列表 +ListUrl(string url, string typeName)

        /// <summary>
        /// 获取所有的内容列表
        /// </summary>
        public void ListUrl(string url, string typeName)
        {
            try
            {
                string baseUrl = "https://www.sanwen.net";
                string docText = HtmlHelper.GetHtmlContent(baseUrl + url);
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(docText);
                if (doc == null)
                    return;
                var li = doc.DocumentNode.SelectNodes("//div[@class=\"categorylist\"]//ul//li");
                if (li != null)
                {
                    for (int i = 1; i < li.Count(); i++)
                    {
                        var outerHtml = li[i].SelectSingleNode("/html[1]/body[1]/div[1]/div[2]/div[1]/div[1]/div[4]/ul[1]/li[" + i + "]/h3[1]/a[2]");
                        if (outerHtml != null)
                        {
                            var href = HtmlHelper.GetTitleContent(outerHtml.OuterHtml, "a", "href");
                            if (href.NotNullOrWhiteSpace())
                            {
                                string pat = null;
                                pat = baseUrl + href;
                                if (spcontentService.CanInsert(pat))
                                {
                                    qdm.Enqueue(new DM { Url = pat, pageName = typeName });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logser.Error("散文网爬虫-ListUrl: " + ex.Message + "");
            }
        }
        #endregion
    }
    public class DM
    {
        public string Url { get; set; }
        public string pageName { get; set; }
    }
}
